<?php

namespace App\Http\Resources;

use App\Models\BookType;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Str;

class BookChaptersResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $images = array_filter(explode(',', $this->imgs));
//        dd($this->imgs,$images);
        $image_list = [];
        foreach ($images as $image) {
            $image_list[] = dealUrl($image);
        }
        return [
            'id' => (int)$this->id,
            'name' => $this->name,
            'description' => $this->description,
            'content' => $this->content,
            'imgs' => $images,
            'image_list' => $image_list,
            'videos' => json_decode($this->videos, true),
            'sort' => (int)$this->sort,
            'book' => new BooksResource($this->whenLoaded('book')),
            'created_at' => (string)$this->created_at,
            'updated_at' => (string)$this->updated_at,
        ];
    }
}
